import java.awt.*;
import java.awt.event.InputEvent;

public class DetectedColorChangedAndClick {
    public static void main(String[] args) throws Exception {
        System.out.println("5s 后开始检测");
        Thread.sleep(5000);
        System.out.println("开始检测");
        Robot robot = new Robot();

        // target coordinate
        int x = 368;
        int y = 79;

        // 检测举手
        int x2 = 1574;
        int y2 = 996;

        int x3 = 107;
        int y3 = 988;

        // save the initial color
        Color initialColor = robot.getPixelColor(x, y);

        // wait for color change
        while (robot.getPixelColor(x, y).equals(initialColor)
                || robot.getPixelColor(x-10, y).equals(initialColor)
                || robot.getPixelColor(x-20, y).equals(initialColor)
                || robot.getPixelColor(x-30, y).equals(initialColor)
                || robot.getPixelColor(x+10, y).equals(initialColor)
                || robot.getPixelColor(x+20, y).equals(initialColor)
                || robot.getPixelColor(x+30, y).equals(initialColor)
        ) {
            Thread.sleep(50);
        }

        // click the mouse
        // 没加这个循环之前测试结果一直都不对, 后面加了循环循环了10次鼠标才能正确移动到这个位置
        for (int i = 0; i < 10; i++) {
            robot.mouseMove(x, y);
        }
        // System.out.println(MouseInfo.getPointerInfo().getLocation().getX() + " , " + MouseInfo.getPointerInfo().getLocation().getY());
        robot.mousePress(InputEvent.BUTTON1_MASK);
        // robot.delay(100);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        System.out.println("进入直播成功");


        // save the initial color
        Color initialColor2 = robot.getPixelColor(x2, y2);

        // wait for color change
        while (robot.getPixelColor(x2, y2).equals(initialColor2)
                || robot.getPixelColor(x2-10, y2).equals(initialColor2)
                || robot.getPixelColor(x2-20, y2).equals(initialColor2)
                || robot.getPixelColor(x2+10, y2).equals(initialColor2)
                || robot.getPixelColor(x2+20, y2).equals(initialColor2)
                || robot.getPixelColor(x2+30, y2).equals(initialColor2)
        ) {
            Thread.sleep(50);
        }
        Thread.sleep(50);
        // click the mouse
        for (int i = 0; i < 10; i++) {
            robot.mouseMove(x2, y2);
        }
        // System.out.println(MouseInfo.getPointerInfo().getLocation().getX() + " , " + MouseInfo.getPointerInfo().getLocation().getY());
        while (true) {
            robot.mousePress(InputEvent.BUTTON1_MASK);
            // robot.delay(100);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
            Color initialColor3 = robot.getPixelColor(435, 967);

            if (robot.getPixelColor(x3, y3).equals(initialColor3)
                    || robot.getPixelColor(x3-10, y3).equals(initialColor3)
                    || robot.getPixelColor(x3-20, y3).equals(initialColor3)
                    || robot.getPixelColor(x3+10, y3).equals(initialColor3)
                    || robot.getPixelColor(x3+20, y3).equals(initialColor3)) {
                break;
            }
        }
        System.out.println("举手成功");
    }
}
